package org.intrasearch.frontend.views;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.ExternalLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import org.intrasearch.core.Artifact;
import org.intrasearch.core.FileArtifact;
import org.intrasearch.core.ResourceArtifact;

import java.util.List;

public class ResultsPanel extends Panel
{
    public ResultsPanel(String id, IModel<List<Artifact>> model)
    {
        super(id, model);
        add(new ListView<Artifact>("results", model)
        {
            @Override
            protected void populateItem(ListItem<Artifact> artifactListItem)
            {
                if (!(artifactListItem.getModelObject() instanceof ResourceArtifact))
                {
                    return;
                }
                ResourceArtifact resourceArtifact = (ResourceArtifact) artifactListItem.getModelObject();
                artifactListItem.add(new Label("fileName", resourceArtifact.getName()));
                String fileSizeId = "fileSize";
                if (resourceArtifact instanceof FileArtifact)
                {
                    FileArtifact fileArtifact = (FileArtifact) resourceArtifact;
                    String fileSize = "[" + fileArtifact.getFileSize() + " bytes]";
                    artifactListItem.add(new Label(fileSizeId, fileSize));
                }
                else
                {
                    Label fileSizeLabel = new Label(fileSizeId);
                    fileSizeLabel.setVisible(false);
                    artifactListItem.add(fileSizeLabel);
                }
                artifactListItem.add(new ListView<String>("urls", new PropertyModel<List<String>>(artifactListItem.getModel(), "urls"))
                {
                    @Override
                    protected void populateItem(ListItem<String> urlListItem)
                    {
                        String href = urlListItem.getModelObject();
                        urlListItem.add(new ExternalLink("url", href, href));
                    }
                });
            }
        });
    }
}
